
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增地址-地址管理</title>
    {include file="public/head" /}

<body>
<div class="mfc cbox headtop fl">
    <div class="sreach ret mfc ">
        <span class="fa fa-angle-left pabs-l-box fa-2x tc" style="width:1.6em;" onclick="history.go(-1)"></span>
        <p class="tc" style="font-size: 18px;">新增地址</p>
<!--        <span class="pabs-r-box c-red tc" style="padding: 0 0.6em;"> 保存 </span>-->
    </div>
</div>
<div class="mfc adress bc-back fl " style="padding-top: 1em;">
    <div class="cbox gap">
        <div class="adress-adder">
            <div class="mfc fl">
                <span class="tips fl">收货人</span><input type="text" name="name" value="{$info.name|default=''}" placeholder="请输入收货人的姓名">
            </div>
            <div class="mfc fl">
                <span class="tips fl">手机号码</span><input type="text" name="mobile" value="{$info.mobile|default=''}" placeholder="请输入收货人的手机号">
            </div>
        </div>
    </div>
    <div class="cbox gap">
        <div class="adress-adder">
            <div class="mfc fl">
                <span class="tips fl">选择省份</span>
                <select name="prov">
                    <option value="">请选择省份</option>
                    {volist name="prov" id="vo"}
                    <option value="{$vo.name}" {eq name="$vo.name" value="$info.prov|default=''"}selected{/eq}>{$vo.name}</option>
                    {/volist}
                </select>
            </div>
            <div class="mfc fl">
                <span class="tips fl">选择城市</span>
                <select name="city">
                    <option value="">请选择城市</option>
                    {volist name="city" id="vo"}
                    <option value="{$vo.name}" {eq name="$vo.name" value="$info.city|default=''"}selected{/eq}>{$vo.name}</option>
                    {/volist}
                </select>
            </div>
            <div class="mfc fl">
                <span class="tips fl">选择县/区</span>
                <select name="area">
                    <option value="">请选择县/区</option>
                    {volist name="area" id="vo"}
                    <option value="{$vo.name}" {eq name="$vo.name" value="$info.area|default=''"}selected{/eq}>{$vo.name}</option>
                    {/volist}
                </select>
            </div>
<!--            <div class="mfc fl">-->
<!--                <span class="tips fl">选择街道</span>-->
<!--                <select name="user">-->
<!--                    <option value="">请选择街道</option>-->
<!--                    <option value="">XXX街道</option>-->
<!--                </select>-->
<!--            </div>-->
            <div class="mfc fl">
                <span class="tips fl">详细地址</span><input type="text" name="address" value="{$info.address|default=''}" placeholder="请详细地址">
            </div>
        </div>
    </div>
</div>
<div style="height: 100px;">
1
</div>

<div class="mfc  pfix-b-box">
    <div class="cbox order-option tci card">
        <button class="btnbox oval " onclick="save()">保存</button>
    </div>
</div>

</body>
</html>
<script>
    $('[name="prov"]').change(function () {
        var code = $('[name="prov"]').val();
        $.ajax({
            type: "POST", //用POST方式传输
            dataType: "json", //数据格式:JSON
            data: {code: code},
            url: "{:url('ProvCityArea/get_city')}", //目标地址
            async: false,
            beforeSend: function () {
                $('[name="city"]').html('<option value="">请选择市</option>');
                $('[name="area"]').html('<option value="">请选择区</option>');
            },
            success: function (ret) {
                if (ret.code == 200) {
                    $.each(ret.data, function (i, item) {
                        $('[name="city"]').append('<option value="' + item['name'] + '">' + item['name'] + '</option>');
                    });
                }
            }
        });

    });


    $('[name="city"]').change(function () {

        var code = $('[name="city"]').val();
        $.ajax({
            type: "POST", //用POST方式传输
            dataType: "json", //数据格式:JSON
            data: {code: code},
            url: "{:url('ProvCityArea/get_area')}", //目标地址
            async: false,
            beforeSend: function () {
                $('[name="area"]').html('<option value="">请选择区</option>');
            },
            success: function (ret) {

                if (ret.code == 200) {

                    $.each(ret.data, function (i, item) {
                        $('[name="area"]').append('<option value="' + item['name'] + '">' + item['name'] + '</option>');

                    });
                }
            }
        });

    });


    function save() {

        var name = $('[name="name"]').val();
        if (!name) {
            layer.msg('请填写收货人');
            return;
        }
        var mobile = $('[name="mobile"]').val();
        if (!mobile) {
            layer.msg('请填写手机号');
            return;
        }
        var prov = $('[name="prov"]').val();
        if (!prov) {
            layer.msg('请选择省');
            return;
        }
        var city = $('[name="city"]').val();
        if (!city) {
            layer.msg('请选择市');
            return;
        }
        var area = $('[name="area"]').val();
        if (!area) {
            layer.msg('请选择区');
            return;
        }
        var address = $('[name="address"]').val();
        if (!address) {
            layer.msg('请填写收件地址');
            return;
        }
        var id= "{:input('id')}";
        if(id ){
            $.ajax({
                type: "POST", //用POST方式传输
                dataType: "json", //数据格式:JSON
                data: { id:id,address: address,prov:prov,city:city,area:area,name:name,mobile:mobile},
                url: "{:url('update')}", //目标地址
                async: false,
                success: function (ret) {
                    layer.msg(ret.message);
                    if(ret.code == 200){
                        setTimeout(function () {
                            window.location.href = "{:url('index')}";
                        },300);
                    }
                }

            });
        }else{
            $.ajax({
                type: "POST", //用POST方式传输
                dataType: "json", //数据格式:JSON
                data: { address: address,prov:prov,city:city,area:area,name:name,mobile:mobile},
                url: "{:url('create')}", //目标地址
                async: false,
                success: function (ret) {
                    layer.msg(ret.message);
                    if(ret.code == 200){
                        setTimeout(function () {
                            window.location.href = "{:url('index')}";
                        },300);
                    }
                }

            });
        }


    }

</script>